package com.yuuzy.view;

import com.yuuzy.dao.FundDAO;
import com.yuuzy.model.Fund;
import com.yuuzy.utils.StringUtils;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

public class ManageFund extends JInternalFrame {
    private JTable fund_table;

    private FundDAO fundDAO = new FundDAO();
    private ButtonGroup group = new ButtonGroup();
    private JTextField QF_nameText;
    private JTextField fund_idText;
    private JTextField fund_nameText;
    private JTextField fund_typeText;
    private JTextField fund_risklevelText;
    private JTextField fund_managerText;
    private JTextField fund_amountText;
    private JRadioButton fund_upRB;
    private JRadioButton fund_downRB;

    /**
     * Create the frame.
     */
    public ManageFund() {
        setClosable(true);
        setIconifiable(true);
        setTitle("基金产品管理");
        setBounds(100, 100, 1000, 1100);

        JScrollPane scrollPane = new JScrollPane();

        JLabel label = new JLabel("基金名称：");
        label.setFont(new Font("宋体", Font.PLAIN, 20));

        QF_nameText = new JTextField();
        QF_nameText.setFont(new Font("宋体", Font.PLAIN, 20));
        QF_nameText.setColumns(10);

        JButton button = new JButton("查询 ");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                queryActionPerformed(e);
            }
        });
        button.setIcon(new ImageIcon(ManageFund.class.getResource("/image/pm-query-16.png")));
        button.setFont(new Font("宋体", Font.PLAIN, 20));

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(null, "\u4FEE\u6539/\u5220\u9664\u57FA\u91D1\u4EA7\u54C1", TitledBorder.LEADING, TitledBorder.TOP, null, SystemColor.textHighlight));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(198)
                                                .addComponent(label, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)
                                                .addComponent(QF_nameText, GroupLayout.PREFERRED_SIZE, 264, GroupLayout.PREFERRED_SIZE)
                                                .addGap(82)
                                                .addComponent(button, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(91)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
                                                        .addComponent(scrollPane)
                                                        .addComponent(panel, GroupLayout.DEFAULT_SIZE, 801, Short.MAX_VALUE))))
                                .addContainerGap(92, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(56)
                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                        .addComponent(label, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(QF_nameText, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
                                .addGap(58)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 296, GroupLayout.PREFERRED_SIZE)
                                .addGap(53)
                                .addComponent(panel, GroupLayout.PREFERRED_SIZE, 510, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(53, Short.MAX_VALUE))
        );

        JLabel label_1 = new JLabel("基金编号：");

        fund_idText = new JTextField();
        fund_idText.setText("");
        fund_idText.setFont(new Font("宋体", Font.PLAIN, 18));
        fund_idText.setEnabled(true);
        fund_idText.setEditable(false);
        fund_idText.setColumns(10);

        JLabel label_2 = new JLabel("基金名称：");

        fund_nameText = new JTextField();
        fund_nameText.setColumns(10);

        JLabel label_3 = new JLabel("基金类型：");

        fund_typeText = new JTextField();
        fund_typeText.setColumns(10);

        JLabel label_4 = new JLabel("风险等级：");

        fund_risklevelText = new JTextField();
        fund_risklevelText.setColumns(10);

        JLabel label_5 = new JLabel("基金管理者：");

        fund_managerText = new JTextField();
        fund_managerText.setColumns(10);

        JLabel label_6 = new JLabel("基金价格：");

        fund_amountText = new JTextField();
        fund_amountText.setColumns(10);

        JLabel label_7 = new JLabel("是否上线：");

        fund_upRB = new JRadioButton("是");
        group.add(fund_upRB);
        fund_upRB.setSelected(true);

        fund_downRB = new JRadioButton("否");
        group.add(fund_downRB);

        JButton btnNewButton = new JButton("修改 ");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                updateActionPerformed(e);
            }
        });
        btnNewButton.setIcon(new ImageIcon(ManageFund.class.getResource("/image/icon_update.png")));

        JButton button_1 = new JButton("删除 ");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                deleteActionPerformed(e);
            }
        });
        button_1.setIcon(new ImageIcon(ManageFund.class.getResource("/image/delete.png")));
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGap(70)
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addComponent(label_4, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)
                                                .addComponent(fund_risklevelText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
                                                        .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(label_1, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE))
                                                .addGap(18)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addComponent(fund_nameText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(34)
                                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(label_5)
                                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                                .addGap(7)
                                                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                                                        .addComponent(label_3, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                                                        .addComponent(label_7, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE))))
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                                .addComponent(fund_upRB)
                                                                                .addGap(74)
                                                                                .addComponent(fund_downRB, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))
                                                                        .addComponent(fund_managerText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(fund_typeText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE)))
                                                        .addComponent(fund_idText, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addComponent(label_6, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addGap(14)
                                                                .addComponent(btnNewButton)
                                                                .addGap(202)
                                                                .addComponent(button_1))
                                                        .addComponent(fund_amountText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE))))
                                .addContainerGap(69, Short.MAX_VALUE))
        );
        gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGap(47)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_1)
                                        .addComponent(fund_idText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(65)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_2)
                                        .addComponent(fund_nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_3)
                                        .addComponent(fund_typeText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(65)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_4)
                                        .addComponent(fund_risklevelText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(fund_managerText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_5))
                                .addGap(68)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_6)
                                        .addComponent(fund_amountText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_7)
                                        .addComponent(fund_upRB)
                                        .addComponent(fund_downRB))
                                .addPreferredGap(ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(btnNewButton)
                                        .addComponent(button_1))
                                .addGap(38))
        );
        panel.setLayout(gl_panel);

        fund_table = new JTable();
        fund_table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                tableMousePressed(me);
            }
        });
        fund_table.setFillsViewportHeight(true);
        fund_table.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "基金编号", "基金名称", "基金类型", "风险等级", "基金管理者", "基金金额", "上线标志位"
                }
        ));
        fund_table.getColumnModel().getColumn(1).setPreferredWidth(101);
        fund_table.getColumnModel().getColumn(2).setPreferredWidth(96);
        fund_table.getColumnModel().getColumn(3).setPreferredWidth(139);
        fund_table.getColumnModel().getColumn(4).setPreferredWidth(123);
        fund_table.getColumnModel().getColumn(5).setPreferredWidth(113);
        scrollPane.setViewportView(fund_table);
        getContentPane().setLayout(groupLayout);

        this.fillTable(new Fund());

    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ManageFund frame = new ManageFund();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 删除事件处理
     *
     * @param evt
     */
    private void deleteActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String fund_id = fund_idText.getText();
        if (StringUtils.isEmpty(fund_id)) {
            JOptionPane.showMessageDialog(null, "请选择要删除的基金产品！");
            return;
        }
        Fund fund = new Fund(StringUtils.changeInt(fund_id));
        int n = JOptionPane.showConfirmDialog(null, "确定要删除该产品吗？");
        if (n == 0) {
            int deleteNum = fundDAO.deleteFund(fund);
            if (deleteNum > 0) {
                JOptionPane.showMessageDialog(null, "删除成功！");
                this.resetValue();
                this.fillTable(new Fund());
            } else {
                JOptionPane.showMessageDialog(null, "删除失败！");
            }
        }
    }

    /**
     * 修改事件处理
     *
     * @param evt
     */
    private void updateActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String fund_id = this.fund_idText.getText();
        if (StringUtils.isEmpty(fund_id)) {
            JOptionPane.showInputDialog(null, "请选择要修改的保险产品！");
            return;
        }

        String fund_name = this.fund_nameText.getText();
        String fund_type = this.fund_typeText.getText();
        String fund_riskleve = this.fund_risklevelText.getText();
        String fund_manager = this.fund_managerText.getText();
        String fund_amount = this.fund_amountText.getText();

        if (StringUtils.isEmpty(fund_name)) {
            JOptionPane.showMessageDialog(null, "基金名称不能为空！");
            return;
        }

        if (StringUtils.isEmpty(fund_type)) {
            JOptionPane.showMessageDialog(null, "基金类型不能为空！");
            return;
        }

        if (StringUtils.isEmpty(fund_riskleve)) {
            JOptionPane.showMessageDialog(null, "风险等级不能为空！");
            return;
        }

        if (StringUtils.isEmpty(fund_manager)) {
            JOptionPane.showMessageDialog(null, "基金管理者不能为空！");
            return;
        }

        if (StringUtils.isEmpty(fund_amount)) {
            JOptionPane.showMessageDialog(null, "基金价格不能为空！");
            return;
        }

        String fund_up = "";
        if (fund_upRB.isSelected()) {
            fund_up = "1";
        } else if (fund_downRB.isSelected()) {
            fund_up = "0";
        }

        if (StringUtils.isEmpty(fund_up)) {
            JOptionPane.showMessageDialog(null, "请选择是否上线该基金！");
            return;
        }

        Fund fund = new Fund(StringUtils.changeInt(fund_id), fund_name, fund_type, fund_riskleve,
                fund_manager, StringUtils.changeDouble(fund_amount), fund_up);
        int insertNum = fundDAO.updateFund(fund);
        if (insertNum > 0) {
            JOptionPane.showMessageDialog(null, "修改成功！");
            this.resetValue();
            this.fillTable(new Fund());
        } else {
            JOptionPane.showMessageDialog(null, "修改失败！");
        }
    }

    //重置表单
    private void resetValue() {
        // TODO Auto-generated method stub
        this.fund_idText.setText("");
        this.fund_nameText.setText("");
        this.fund_typeText.setText("");
        this.fund_risklevelText.setText("");
        this.fund_managerText.setText("");
        this.fund_amountText.setText("");
        this.group.clearSelection();
    }

    /**
     * 表格行点击事件处理
     *
     * @param tme
     */
    private void tableMousePressed(MouseEvent tme) {
        // TODO Auto-generated method stub
        int row = this.fund_table.getSelectedRow();
        this.fund_idText.setText(String.valueOf(fund_table.getValueAt(row, 0)));
        this.fund_nameText.setText(String.valueOf(fund_table.getValueAt(row, 1)));
        this.fund_typeText.setText(String.valueOf(fund_table.getValueAt(row, 2)));
        this.fund_risklevelText.setText(String.valueOf(fund_table.getValueAt(row, 3)));
        this.fund_managerText.setText(String.valueOf(fund_table.getValueAt(row, 4)));
        this.fund_amountText.setText(String.valueOf(fund_table.getValueAt(row, 5)));
        String up = String.valueOf(fund_table.getValueAt(row, 6));
        if ("1".equals(up)) {
            this.fund_upRB.setSelected(true);
        } else if ("0".equals(up)) {
            this.fund_downRB.setSelected(true);
        }
    }

    /**
     * 查询事件处理
     *
     * @param evt
     */
    private void queryActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String fund_name = this.QF_nameText.getText();
        Fund fund = new Fund();
        fund.setFund_name(fund_name);
        this.fillTable(fund);
    }

    /**
     * 初始化表格
     *
     * @param fund
     */
    private void fillTable(Fund fund) {
        DefaultTableModel dtm = (DefaultTableModel) fund_table.getModel();
        dtm.setRowCount(0);//设置为0行，清空表格
        List<Fund> fundList = fundDAO.queryFund(fund);
        for (Fund rs : fundList) {
            Vector v = new Vector();
            v.add(rs.getFund_id());
            v.add(rs.getFund_name());
            v.add(rs.getFund_type());
            v.add(rs.getFund_risklevel());
            v.add(rs.getFund_manager());
            v.add(rs.getFund_amount());
            v.add(rs.getFund_up());
            dtm.addRow(v);
        }
    }
}
